package cn.powerone.scm.service.impl;

import java.util.Date;
import java.util.List;

import cn.powerone.common.exception.ServiceException;
import cn.powerone.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import cn.powerone.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import cn.powerone.scm.domain.ScmCustomerContacts;
import cn.powerone.scm.mapper.ScmCustomerMapper;
import cn.powerone.scm.domain.ScmCustomer;
import cn.powerone.scm.service.IScmCustomerService;

/**
 * 客户Service业务层处理
 * 
 * @author PowerOne
 * @date 2025-06-23
 */
@Service
public class ScmCustomerServiceImpl implements IScmCustomerService 
{
    @Autowired
    private ScmCustomerMapper scmCustomerMapper;

    /**
     * 查询客户
     * 
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public ScmCustomer selectScmCustomerById(Long id)
    {
        return scmCustomerMapper.selectScmCustomerById(id);
    }

    /**
     * 查询客户列表
     * 
     * @param scmCustomer 客户
     * @return 客户
     */
    @Override
    public List<ScmCustomer> selectScmCustomerList(ScmCustomer scmCustomer)
    {
        return scmCustomerMapper.selectScmCustomerList(scmCustomer);
    }

    /**
     * 新增客户
     * 
     * @param scmCustomer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int insertScmCustomer(ScmCustomer scmCustomer)
    {
        scmCustomer.setCreateTime(DateUtils.getNowDate());
        int rows = scmCustomerMapper.insertScmCustomer(scmCustomer);
        insertScmCustomerContacts(scmCustomer);
        return rows;
    }

    /**
     * 修改客户
     * 
     * @param scmCustomer 客户
     * @return 结果
     */
    @Transactional
    @Override
    public int updateScmCustomer(ScmCustomer scmCustomer)
    {
        scmCustomer.setUpdateTime(DateUtils.getNowDate());
        scmCustomerMapper.deleteScmCustomerContactsByCustomerId(scmCustomer.getId());
        insertScmCustomerContacts(scmCustomer);
        return scmCustomerMapper.updateScmCustomer(scmCustomer);
    }

    /**
     * 批量删除客户
     * 
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmCustomerByIds(Long[] ids)
    {
        scmCustomerMapper.deleteScmCustomerContactsByCustomerIds(ids);
        return scmCustomerMapper.deleteScmCustomerByIds(ids);
    }

    /**
     * 删除客户信息
     * 
     * @param id 客户主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteScmCustomerById(Long id)
    {
        scmCustomerMapper.deleteScmCustomerContactsByCustomerId(id);
        return scmCustomerMapper.deleteScmCustomerById(id);
    }

    /**
     * 新增联系人信息
     * 
     * @param scmCustomer 客户对象
     */
    public void insertScmCustomerContacts(ScmCustomer scmCustomer)
    {
        List<ScmCustomerContacts> scmCustomerContactsList = scmCustomer.getScmCustomerContactsList();
        Long id = scmCustomer.getId();
        if (StringUtils.isNotNull(scmCustomerContactsList))
        {
            List<ScmCustomerContacts> list = new ArrayList<ScmCustomerContacts>();
            for (ScmCustomerContacts scmCustomerContacts : scmCustomerContactsList)
            {
                scmCustomerContacts.setCustomerId(id);
                list.add(scmCustomerContacts);
            }
            if (list.size() > 0)
            {
                scmCustomerMapper.batchScmCustomerContacts(list);
            }
        }
    }

    /** 导入数据
     * @param scmCustomer 导入询价Batch列表
     */

    public String importData(List<ScmCustomer> scmCustomer, boolean updateSupport, String operName){
        if (StringUtils.isNull(scmCustomer) || scmCustomer.size() == 0)
        {
            throw new RuntimeException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        ScmCustomer customerInq = new ScmCustomer();
        long numberOfInq = 0;
        for (ScmCustomer item : scmCustomer)
        {
            if(item.getCustomerNo() != null && !item.getCustomerNo().isEmpty() && !updateSupport){
                customerInq.setCustomerNo(item.getCustomerNo());
                numberOfInq = scmCustomerMapper.selectScmCustomerList(customerInq).size();
                if(numberOfInq > 0){
                    continue;
                }
            }
            try
            {
                scmCustomerMapper.insertScmCustomer(item);
                successNum++;
            } catch (Exception e)
            {
                failureNum++;
            }
        }
        if (failureNum > 0){
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int selectCustomerCountByMonth(Date yesterday) {
        return scmCustomerMapper.selectCustomerCountByMonth( yesterday);
    }

    @Override
    public int selectCustomerCountByYear(Date yesterday) {
        return scmCustomerMapper.selectCustomerCountByYear( yesterday);
    }

    @Override
    public int selectCustomerCountAll() {
        return scmCustomerMapper.selectCustomerCountAll();
    }

}
